import 'package:eq/src/core.dart';
import 'package:eq/src/interpreter.dart';
import 'package:eq/src/utils.dart';

void addComplexFunctions(Module module) {
  addNativeFunction("add", _add, module);
  addNativeFunction("sub", _add, module);
  addNativeFunction("mul", _add, module);
  addNativeFunction("div", _add, module);
  addNativeFunction("add", _add, module);
}

Value _add(List<Value> args) {
  if (args.isEmpty) return Value(ValueType.NULL_VALUE, null);
  Complex complex = Complex(0, 0);
  for (var i in args) {
    if (i.type == ValueType.INT_VALUE ||
        i.type == ValueType.DOUBLE_VALUE ||
        i.type == ValueType.COMPLEX_VALUE) {
      complex += i.value;
    } else {
      return Value(ValueType.NULL_VALUE, null);
    }
  }

  if (complex.b == 0) {
    return Value(ValueType.DOUBLE_VALUE, complex.a);
  }
  return Value(ValueType.COMPLEX_VALUE, complex);
}
